package forever.com.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AutoIncrement {
	private String tableName = "";
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	public AutoIncrement(String tableName){
		this.tableName = tableName;
		conn = JDBCUtil.getConnection();
	}
	
	public int getNextId(){
		int nextId = 0;
		try {
			ps = conn.prepareStatement("show table status like ?");
			ps.setString(1, tableName);
			rs = ps.executeQuery();
			if(rs.next()){
				nextId = rs.getInt("auto_increment");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
				JDBCUtil.Close(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return nextId;
	}
}
